package cn.com.dycc.study_manage.integral.service;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.entity.Pb_Integral_Dept_LogCount;
import cn.com.dycc.study_manage.integral.dao.Pb_Integral_Dept_LogDao;
import cn.com.dycc.study_manage.integral.entity.Pb_Integral_Dept_Log;
import cn.com.dycc.study_manage.integral.param.Pb_Integral_Dept_LogParam;
import cn.com.dycc.util.ConfigRegex;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;

@Service("pb_Integral_Dept_LogService")
@Transactional
public class Pb_Integral_Dept_LogServiceImpl implements Pb_Integral_Dept_LogService{

	@Resource
	private Pb_Integral_Dept_LogDao pb_Integral_Dept_LogDao;
	
	
	public JsonObjectResult findPb_Integral_Dept_LogByParam(Pb_Integral_Dept_LogParam param,
			HttpServletRequest request) throws Pb_Integral_Dept_LogNotFoundException {
		List<Pb_Integral_Dept_Log> lists = new ArrayList<Pb_Integral_Dept_Log>();
		
		checkParam(param);
		param = realParam(param, request);
		String count = pb_Integral_Dept_LogDao.getPb_Integral_Dept_LogCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_Dept_LogParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_Dept_LogDao.findPb_Integral_Dept_LogByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	/**
	 * 实际参数
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	public Pb_Integral_Dept_LogParam realParam(Pb_Integral_Dept_LogParam param, HttpServletRequest request) {
		if(param.getLog_des()!=null&&!param.getLog_des().trim().isEmpty()){
			param.setLog_des(StringUtil.format_mh_str(param.getLog_des()));
		}
		if(param.getOrder()==null||param.getOrder().isEmpty()||"1".equals(param.getOrder())){
			param.setOrder("p.create_date desc");
		}else if("2".equals(param.getOrder())){
			param.setOrder("p.create_date asc");
			
		}else{
			param.setOrder(null);//手动防止SQL注入
		}
		return param;
	}
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Integral_Dept_LogParam param) {
		if (param == null) {
			throw new Pb_Integral_Dept_LogNotFoundException("未登录或超时,请重新登录");
		}
		long start=0;
		long end=0;
		StringBuilder builder=new StringBuilder();
		if(param.getCreate_date()!=null&&!param.getCreate_date().trim().isEmpty()){
			if(!param.getCreate_date().matches(ConfigRegex.DATEREGEX)){
				builder.append("开始时间格式不正确,应为YYYY-MM-DD;");
			}else{
				start=DateUtil.format_StringToDate(param.getCreate_date()).getTime();
			}
			
		}
		if(param.getCreate_date_e()!=null&&!param.getCreate_date_e().trim().isEmpty()){
			if(!param.getCreate_date_e().matches(ConfigRegex.DATEREGEX)){
				builder.append("开始时间格式不正确,应为YYYY-MM-DD;");
			}else {
				end=DateUtil.format_StringToDate(param.getCreate_date_e()).getTime();
				if(start-end>0){
					builder.append("开始时间大于结束时间;");
				}
			}
			
		}
		
		if(!"".equals(builder.toString())){
			throw new Pb_Integral_Dept_LogNotFoundException(builder.toString());
		}

	}

	

	public JsonObjectResult findPb_Integral_Dept_LogCountByParam(Pb_Integral_Dept_LogParam param, HttpServletRequest request)
			throws Pb_Integral_Dept_LogNotFoundException {
		
		List<Pb_Integral_Dept_LogCount> lists = new ArrayList<Pb_Integral_Dept_LogCount>();
		
		checkParam(param);
		param = realParam(param, request);
		String count = pb_Integral_Dept_LogDao.getStatisticsCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Integral_Dept_LogParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Integral_Dept_LogDao.findPb_Integral_Dept_LogCountByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
}
